<!DOCTYPE html>
<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="breadcrumbs_container">
	{{Breadcrumbs::render('admin.pricesetting_master') }}</div>
@extends('layouts.admin_common') @section('content')
<div class="system_tab_line"></div>
<head>
<title>価格設定マスターー</title>

<script>
    $(document).ready(function(){
    	jQuery.validator.addMethod("isCode", function(value, element) {
    	    var tel = /^[1-9][0-9]{0,8}$/;
    	    return this.optional(element) || (tel.test(value));
    	}, "頭文字が’０’を除く数字に入力してお願いします。");

    	// 0到100间数字
    	jQuery.validator.addMethod("isPercent", function(value, element) {
    	    var tel = /^(([^0][\d]?)|0|100)$/;
    	    return this.optional(element) || (tel.test(value));
    	}, "% 0～100間の数字に入力してお願いします。");

    	$.extend($.validator.messages, {
    	    required: '*入力必須です'
    	  });

		 $('#priceForm').validate();

    	$("#group_id").blur(function(){

    		if (this.value != ""){
        		$.post("<?php echo url("admin/setprice/"); ?>/getGrpId",{'_token':'{{csrf_token()}}','group_id':$("#group_id").val()},function(data) //第二个参数要传token的值 再传参数要用逗号隔开
		        {
    				if(data>0){
    					alert("グループもう存在しています。");

    				}
		        });
    	     }
    	});

    	$("input[type='button']").click(function(){
        	if(this.id=='create'){

        		$("input[type='text']").each(function() {
        			$(this).val('');
        			$(this).attr("disabled",false);

        		});
        		$("#group_id").focus();

        	}else if(this.id=='update'){

            var ifo=$(this).parent().children(0).val().split(',');
            $("#groupId").val(ifo[0]);
             for(var i=0;i<ifo.length;i++){
            	 $("input[type='text']")[i].value=(ifo[i]==0?'':ifo[i]);

            }
             $("input[type='text']").each(function() {
     			$(this).attr("disabled",false);
     		});
     		$("#group_id").attr("disabled",true);
        	}else {

           if(this.id=='delete'){
        	   if (false === confirm('削除しますか？') )return;
            	var ifo=$(this).parents().children(0).val().split(',');
            	$("#groupId").val(ifo[0]);
        	}

			var newUrl = "<?php echo url("admin/setprice/"); ?>/"+this.id;    //设置新提交地址
	        $("#priceForm").attr('action',newUrl);    //通过jquery为action属性赋值
	        $("#priceForm").submit();

}
		});
    });
    </script>
</head>
<?php if(count($allInfo[0])):?>
<form id="priceForm" class="form-inline" method="post">

	<table class="table table-condensed">
		<tr>
			<td>グループ</td> @foreach ($allInfo[0] as $pricesettingInfo)
			<td>{{$pricesettingInfo->category_name}}</td> @endforeach
		</tr>

		@foreach ($allInfo[1] as $priceGroupInfo)
		<tr>
			<td>{{$priceGroupInfo[0]}}</td> @foreach ($priceGroupInfo[1] as
			$priceInfo)
			<td><?php

    if ($priceInfo->percent != null) {
        echo ($priceInfo->percent * 100) . '%';
    }
    ?></td> @endforeach
			<td><input type="hidden" id="priceGroIfo"
				value="<?php
    $a = $priceGroupInfo[0];
    foreach ($priceGroupInfo[1] as $priceInfo) {
        $a = $a . ',' . ($priceInfo->percent * 100);
    }
    echo $a?>"> <input type="button" value="修正" id="update"
				class="btn btn-primary button_normal" /><input type="button"
				id="delete" value="削除" class="btn btn-primary button_normal" /></td>
		</tr>
		@endforeach
	</table>

	<table class="table table-condensed">
		<tr>
			<td align="right"><input type="button" value="新規" id="create"
				class="btn btn-primary button_normal"></td>

		</tr>
	</table>

	<table class="table table-condensed">
		<tr>
			<td align="center">グループ</td>
		    <?php
    $b = '';
    foreach ($allInfo[0] as $pricesettingInfo) {
        if ($b == '') {
            $b = $pricesettingInfo->category_id;
        } else {
            $b = $b . ',' . ($pricesettingInfo->category_id);
        }
        echo '<td align="center">' . $pricesettingInfo->category_name . '</td><td ></td>';
    }

    echo '<input type="hidden" name="catIds"  value="' . $b . '"/>'?>


			<td></td>
		</tr>
		<tr>
			<td align="center"><input type="text" name="group_id" id="group_id" style="width:50px"
				class="required isCode" disabled="disabled">*</td>
		<?php
    $i = 0;
    foreach ($allInfo[0] as $pricesettingInfo) {
        echo '<td align="center"><input style="width:50px"
				type="text" name="priceseInfo[' . $pricesettingInfo->category_id . ']" id="price" class="required isPercent" disabled="disabled" >*</td><td align="left">%</td>';
        $i ++;
    }

    ?>
		</tr>
	</table>
	<input type="hidden" name="_token" id="csrf-token"
		value="{{ Session::token() }}" /> <input type="hidden" name="groupId"
		id="groupId" />
	<table class="table table-condensed">
		<tr>
			<td align="right"><input type="button" value="保存" id="save"
				class="btn btn-primary button_normal"></td>

		</tr>
	</table>
	<div style="color: #00C957;">
	<?php
    if (! empty(session('mesIfo'))) {
        echo (session('mesIfo'));

        session()->forget('mesIfo');
    }
    ?>
	</div>
</form>
<?php else:?>
<div>
	<?php
    echo 'カテゴリーマスタを先に新規して下さい。';
    ?>
	</div>
<?php endif;?>
@endsection